/******************************************************************************\
 * Filename   : enc_vpss.c
 * Copyright  : 
 * Created    : 2018-10-31 by litao
 * Description:
 * 
\******************************************************************************/
#include "enc_common.h"

/*****************************************************************************
* function : start vpss. VPSS chn with frame
*****************************************************************************/
HI_S32 enc_vpss_start(HI_S32 chan_num, stENC_VIDSET *p_videoSet)
{
    VPSS_GRP VpssGrp;
    VPSS_CHN VpssChn;
    VPSS_GRP_ATTR_S stGrpAttr;
    VPSS_CHN_ATTR_S stChnAttr;
	VPSS_CHN_MODE_S stChnMode;
    VPSS_GRP_PARAM_S stVpssParam;
    HI_S32 s32Ret;


	memset(&stGrpAttr, 0, sizeof(stGrpAttr));
	memset(&stChnAttr, 0, sizeof(stChnAttr));
	memset(&stVpssParam, 0, sizeof(stVpssParam));
    /*** Set Vpss Grp Attr ***/
	stGrpAttr.u32MaxW = 1920;//p_videoSet->vid_width;
	stGrpAttr.u32MaxH = 1080;//p_videoSet->vid_height;
    stGrpAttr.enPixFmt = ENC_PIXEL_FORMAT;
    stGrpAttr.bIeEn = HI_FALSE;
    stGrpAttr.bNrEn = HI_TRUE;
    stGrpAttr.bDciEn = HI_FALSE;
    stGrpAttr.bHistEn = HI_FALSE;
    stGrpAttr.bEsEn = HI_FALSE;
    stGrpAttr.enDieMode = VPSS_DIE_MODE_NODIE;

    VpssGrp = chan_num;
	
    /*** create vpss group ***/
    s32Ret = HI_MPI_VPSS_CreateGrp(VpssGrp, &stGrpAttr);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("HI_MPI_VPSS_CreateGrp failed with %#x!\n", s32Ret);
        return HI_FAILURE;
    }

    /*** set vpss param ***/
    s32Ret = HI_MPI_VPSS_GetGrpParam(VpssGrp, &stVpssParam);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("failed with %#x!\n", s32Ret);
        return HI_FAILURE;
    }
    
    //stVpssParam.u32IeStrength = 0;
    s32Ret = HI_MPI_VPSS_SetGrpParam(VpssGrp, &stVpssParam);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("failed with %#x!\n", s32Ret);
        return HI_FAILURE;
    }

    /*** enable vpss chn, with frame ***/
    VpssChn = 0;
    /* Set Vpss Chn attr */
    stChnAttr.bSpEn = HI_FALSE;
    stChnAttr.bUVInvert = HI_FALSE;
    stChnAttr.bBorderEn = HI_FALSE;
    /*stChnAttr.stBorder.u32Color = 0xffffff;
    stChnAttr.stBorder.u32LeftWidth = 2;
    stChnAttr.stBorder.u32RightWidth = 2;
    stChnAttr.stBorder.u32TopWidth = 2;
    stChnAttr.stBorder.u32BottomWidth = 2;*/
    
    s32Ret = HI_MPI_VPSS_SetChnAttr(VpssGrp, VpssChn, &stChnAttr);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("HI_MPI_VPSS_SetChnAttr failed with %#x\n", s32Ret);
        return HI_FAILURE;
    }
    
	s32Ret = HI_MPI_VPSS_GetChnMode(VpssGrp, VpssChn, &stChnMode);
	if (s32Ret != HI_SUCCESS)
	{
		enc_err("%s failed with %#x\n", __FUNCTION__, s32Ret);
		return HI_FAILURE;
	}

	stChnMode.enChnMode = VPSS_CHN_MODE_USER;
	stChnMode.bDouble = HI_FALSE;
	stChnMode.enPixelFormat = ENC_PIXEL_FORMAT;
	stChnMode.stAspectRatio.enMode = ASPECT_RATIO_AUTO;
	stChnMode.stAspectRatio.stVideoRect.s32X = 0;
	stChnMode.stAspectRatio.stVideoRect.s32Y = 0;
	stChnMode.stAspectRatio.stVideoRect.u32Height = p_videoSet->vid_height;
	stChnMode.stAspectRatio.stVideoRect.u32Width = p_videoSet->vid_width;
	stChnMode.u32Width = p_videoSet->vid_width;
	stChnMode.u32Height = p_videoSet->vid_height;
	stChnMode.enCompressMode = COMPRESS_MODE_NONE;

	s32Ret = HI_MPI_VPSS_SetChnMode(VpssGrp, VpssChn, &stChnMode);
	if (s32Ret != HI_SUCCESS)
	{
		enc_err("%s failed with %#x\n", __FUNCTION__, s32Ret);
		return HI_FAILURE;
	}


    s32Ret = HI_MPI_VPSS_EnableChn(VpssGrp, VpssChn);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("HI_MPI_VPSS_EnableChn failed with %#x\n", s32Ret);
        return HI_FAILURE;
    }
    
    /*** start vpss group ***/
    s32Ret = HI_MPI_VPSS_StartGrp(VpssGrp);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("HI_MPI_VPSS_StartGrp failed with %#x\n", s32Ret);
        return HI_FAILURE;
    }
	
    return HI_SUCCESS;
}

/*****************************************************************************
* function : disable vi dev
*****************************************************************************/
HI_S32 enc_vpss_stop(HI_S32 chan_num)
{
    HI_S32 s32Ret = HI_SUCCESS;
    VPSS_GRP VpssGrp = chan_num;
    VPSS_CHN VpssChn = 0;

    s32Ret = HI_MPI_VPSS_StopGrp(VpssGrp);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("failed with %#x!\n", s32Ret);
        return HI_FAILURE;
    }

    s32Ret = HI_MPI_VPSS_DisableChn(VpssGrp, VpssChn);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("failed with %#x!\n", s32Ret);
        return HI_FAILURE;
    }

    s32Ret = HI_MPI_VPSS_DestroyGrp(VpssGrp);
    if (s32Ret != HI_SUCCESS)
    {
        enc_err("failed with %#x!\n", s32Ret);
        return HI_FAILURE;
    }

    return HI_SUCCESS;
}


